<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs2.model.*, bbs2.dao.*, bbs2.service.*, bbs2.util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="../_checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="../_header.jsp" %>
<script src="/bbs2/res/se2/js/HuskyEZCreator.js" type="text/javascript"></script>
<script src="/bbs2/res/js/mySE2.js" type="text/javascript"></script>
<style>
    input[name=title] { width:700px; border-style:groove; margin:2px; }
    form select { margin-right: 100px; }
    textarea { width: 766px; height: 600px; display: none; }
    iframe { width: 100%; height: 600px; border: 1px solid red; }
</style>
<script>
$(function() {
    $("button[type=submit]").click(function() {
        smartEditor_syncData();
    })
})
</script>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int articleId = param.getInt("aid", 0);
    int boardId = param.getInt("bid", 0);
    String cmd = param.getString("cmd", "");
    String errMsg = null;

    String title, body;
    int category;
    boolean notice;
    Board board = BoardDAO.selectById(boardId);
    ArrayList<ArticleCategory> categories = ArticleCategoryDAO.selectAll(boardId);
    String url = "article.jsp?" + request.getQueryString();
    
    if (request.getMethod().equals("GET")) {
        Article article = ArticleDAO.selectById(articleId, UserService.getCurrentUser(session).getId());
        title = article.getTitle();
        body = article.getBody();
        category = article.getCategoryId();
        notice = article.isNotice();        
    } else {
        title = param.getString("title", "");
        category = param.getInt("category", 0);    
        body = param.getString("body", "");    
        notice = param.getBoolean("notice", false);
        if ("저장".equals(cmd)) {
            if (title.length() > 0) {
                if (body.length() > 0) {
                    ArticleDAO.update(articleId, category, title, body, notice);
                    response.sendRedirect(url);
                    return;
                } else
                    errMsg = "내용을 입력하세요";           
            } else
                errMsg = "제목을 입력하세요";
        }
    }
%>
<body>
<%@ include file="../_menu.jsp" %>  

<div class="container">

    <h1><%= board.getTitle() %></h1>
    <hr />

    <form method="post">
        <div>
            제목: <input type="text" name="title" value="<%= title %>" />            
        </div>
        
        <div class="form-inline">
            분류:
            <select name="category">
                <% for (ArticleCategory ac : categories) { %>
                    <option value="<%= ac.getId() %>" <%= category == ac.getId() ? "selected" : "" %>><%= ac.getName() %></option>
                <% } %>                    
            </select>
            <label>
                공지: <input type="checkbox" name="notice" <%= notice ? "checked" : "" %> />             
            </label>
        </div>
        
        <textarea id="articleBody" name="body" class="smarteditor2" rows="30" cols="100"><%= body %></textarea>

        <% if (board.isFileAttachEnabled()) { %>
            <iframe class="file" src="/bbs2/file/upload_list.jsp?targetType=1&targetId=<%= articleId %>">
            </iframe>
        <% } %>            
        
        <button type="submit" class="btn btn-primary" name="cmd" value="저장"><i class="icon-hdd icon-white"></i> 저장</button>
        <a href="<%= url %>" class="btn">취소</a>
    </form>

    <% if (errMsg != null) { %>
        <div class="alert alert-error"><%= errMsg %></div>
    <% } %>
</div>

<%@ include file="../_footer.jsp" %>  
</body>
</html>
